<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>XmlRpc Test Suite</title>
<script type="text/javascript" src="xmlrpc.js"></script>
<script>
var successes = 0;
var failures = 0;

function assertEquals(testName, expected, actual) {
	document.write("<h3>" + testName + "</h3>");
	if (expected == actual) {
		successes += 1;
		document.write("Pass");
	} else {
		failures += 1;
		var body = "expected " + expected + ", but was " + actual;
		body = body.replace(/&/g, "&amp;");
		body = body.replace(/</g, "&lt;");
		document.write(body + "<br/>");
	}
}
</script>
</head>
<body>
<script type="text/javascript">
// XmlRpcParamTest
var bool = new XMLRpcParam(true);
assertEquals("XMLRpcParam Boolean", "<param><value><boolean>true</boolean></value></param>", bool.xml());
var str = new XMLRpcParam("hello world");
assertEquals("XMLRpcParam Boolean", "<param><value><string>hello world</string></value></param>", str.xml());
var integer = new XMLRpcParam(1234);
assertEquals("XMLRpcParam Boolean", "<param><value><int>1234</int></value></param>", integer.xml());
var dbl = new XMLRpcParam(12.34);
assertEquals("XMLRpcParam Boolean", "<param><value><double>12.34</double></value></param>", dbl.xml());

// XmlRpcResponseParseTest
var parser = new DOMParser();
var string = '<?xml version="1.0"?><methodResponse><params><param><value><string>blah</string></value></param></params></methodResponse>';
assertEquals("XMLRpcResponseParse String", "blah", XMLRpc.parseResponse(parser.parseFromString(string, "text/xml")));
var bool = '<?xml version="1.0"?><methodResponse><params><param><value><boolean>false</boolean></value></param></params></methodResponse>';
assertEquals("XMLRpcResponseParse Boolean", false, XMLRpc.parseResponse(parser.parseFromString(bool, "text/xml")));
var num = '<?xml version="1.0"?><methodResponse><params><param><value><int>12335</int></value></param></params></methodResponse>';
assertEquals("XMLRpcResponseParse Boolean", 12335, XMLRpc.parseResponse(parser.parseFromString(num, "text/xml")));

// XmlRpcTest
var mockHttpReq = {
		open: function (method, server, sync) {
				assertEquals("XMLRpc open(method)", "POST", method);
				assertEquals("XMLRpc open(server)", "http://localhost/xmlrpc", server);
				assertEquals("XMLRpc open(sync)", false, sync);
			},
		send: function (body) {
				this.responseXml = new Object();
				this.status = 200;
			} 
}
var xmlRpc = new XMLRpc(mockHttpReq, "http://localhost/xmlrpc", "encapsulate", [new XMLRpcParam(1.5)]);
xmlRpc.call();

// Summary
document.write("Tests: " + (successes + failures) + ", Successes: " + successes + ", Failures: " + failures + "<br/>");
if (failures == 0) {
	document.write("<h1>All tests passed!</h1>");
} else {
	document.write("<h1>Failure...</h1>");
}
</script>
</body>
</html>